﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using ZSZ.IDAL;
using ZSZ.IService;
using ZSZ.Model;

namespace TestAutoFac.Controllers
{
    public class HomeController : Controller
    {
        public IRoleService RoleService { get; set; }
        // GET: Home
        public ActionResult Index()
        {

            #region 原理代码
            //   ContainerBuilder builder = new ContainerBuilder();
            //            //注入方式 1
            ////builder.RegisterType<UserInfoDal>().As<IUserInfoDal>();
            //        //注入方式 2
            //            builder.RegisterType<UserInfoDal>().AsImplementedInterfaces();

            //            IContainer resolver = builder.Build();
            //            IUserInfoDal userDal = resolver.Resolve<IUserInfoDal>();

            //            return Content(userDal.SayHello());
            #endregion

            #region 程序集注入
            //ContainerBuilder builder = new ContainerBuilder();
            ////Assembly asm = Assembly.Load("ZSZ.DAL");
            //builder.RegisterAssemblyTypes(new Assembly[] { Assembly.Load("ZSZ.DAL") ,
            //    Assembly.Load("ZSZ.DALFactory") ,
            //    Assembly.Load("ZSZ.Service") }).AsImplementedInterfaces().PropertiesAutowired();// 这是最常用的用法！
            //IContainer resolver = builder.Build();
            //IRoleService roleService = resolver.Resolve<IRoleService>();

            //List<string> list = roleService.GetAll().Select(r => r.Name).ToList();
            //return Content(string.Join(",", list)); 
            #endregion

            #region MyRegion
            List<long> list = RoleService.GetAll().Select(r => r.Id).ToList();
            return Content(string.Join(",", list));
            #endregion

            //只有 Autofac 创建的对象，才会自动进行属性赋值
            // TestHelper helper = new TestHelper();  //未将对象引用设置到对象的实例。
            //TestHelper helper=  DependencyResolver.Current.GetService<TestHelper>();
            //return Content(helper.GetRole());
        }
    }
}